package com.mustafaiev.tair.mweather;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.mustafaiev.tair.mweather.database.MWeatherDBHelper;

public class TownAutocompleteActivity extends Activity {

	ListView listView;
	TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.town_activity_layout);

		this.listView = (ListView) this.findViewById(R.id.list);
		this.textView = (TextView) this.findViewById(R.id.resultText);

		Intent intent = this.getIntent();
		if (ForecastListViewActivity.ACTION_SEARCH_TOWN.equals(intent
				.getAction())) {
			this.onSearchRequested();
		}
		if (Intent.ACTION_VIEW.equals(intent.getAction())) {
			Log.i("SearchableDictionary, onCreate()", intent.getAction());
			Uri uri = this.getIntent().getData();
			Cursor cursor = this.managedQuery(uri, null, null, null, null);

			String[] from = new String[] { MWeatherDBHelper.MWEATHER_COLUMN_TOWN_NAME };

			int[] to = new int[] { R.id.town_item };

			SimpleCursorAdapter words = new SimpleCursorAdapter(this,
					R.layout.town_item, cursor, from, to);
			this.listView.setAdapter(words);
		}
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String query = intent.getStringExtra(SearchManager.QUERY);

			this.buildResultList(query);
		}

	}

	/**
	 * Creation date: 4 Sep 2011 04:13:35
	 * 
	 * Method builds result list from {@link Cursor}
	 */
	private void buildResultList(String query) {

		// ContentResolver contentResolver = this.getContentResolver();
		// Cursor cursor = contentResolver.query(
		// MWeatherContentProvider.CONTENT_URI, null, null,
		// new String[] { query }, null);

		Cursor cursor = this.managedQuery(MWeatherContentProvider.CONTENT_URI,
				null, null, new String[] { query }, null);
		if (cursor == null) {
			this.textView.setText(R.string.no_results);
		} else {
			// Specify the columns we want to display in the result
			String[] from = new String[] { MWeatherDBHelper.MWEATHER_COLUMN_TOWN_NAME };

			// Specify the corresponding layout elements where we want the
			// columns to go
			int[] to = new int[] { R.id.town_item };

			// Create a simple cursor adapter for the definitions and apply them
			// to the ListView
			SimpleCursorAdapter words = new SimpleCursorAdapter(this,
					R.layout.town_item, cursor, from, to);
			this.listView.setAdapter(words);

			// Define the on-click listener for the list items
			this.listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// Build the Intent used to open WordActivity with a
					// specific word Uri
					Intent wordIntent = new Intent(
							TownAutocompleteActivity.this
									.getApplicationContext(),
							TownAutocompleteActivity.class);
					Uri data = Uri.withAppendedPath(
							MWeatherContentProvider.CONTENT_URI,
							String.valueOf(id));
					wordIntent.setData(data);
					TownAutocompleteActivity.this.startActivity(wordIntent);
				}
			});
		}
	}

	@Override
	public boolean onSearchRequested() {
		return super.onSearchRequested();
	}

}
